<?php
class main extends spController {
	function index() {
		//echo "Enjoy, Speed of PHP!";

		//$account = spClass("ddz_account");

		// echo "account:".$account->findcount("DATEDIFF(gmt_last_visit,gmt_create)=0");
		$this -> display('main.html');
	}

	function find() {
		$type = $this -> spArgs('type');
		$date_from = $this -> spArgs('date_from');
		$date_to = $this -> spArgs('date_to');
		$account = spClass("ddz_account");
		$args = array('type' => $type);
		if ($date_from <> '') {
			$datelimit = " and date(gmt_create) between '$date_from' and '$date_to'";
		} else {
			$datelimit = '';
		}

		switch ($type) {
			case '0' :
				echo "account:" . $account -> findcount("date(now())-date(gmt_create)=1");
				break;
			case '1' :
				echo "account:" . $account -> findcount("DATEDIFF(gmt_last_visit,gmt_create)=0" . $datelimit);
				break;
			case '2' :
				echo "account:" . $account -> findcount("DATEDIFF(gmt_last_visit,gmt_create)=30" . $datelimit);
				break;
			case '3' :
				echo "account:" . $account -> findcount("DATEDIFF(gmt_last_visit,gmt_create)>30" . $datelimit);
				break;
			default :
				break;
		}

		echo "<a href=" . spUrl(null, 'export', $args, null, FALSE) . ">导出</a>";
		// $account = spClass("ddz_account");
		// echo "account:" . $account -> findcount("DATEDIFF(gmt_last_visit,gmt_create)=0");
	}

	function export() {
		// header("Content-type: text/html; charset=utf-8");
		// header("Content-type:application/vnd.ms-excel");
		// header("content-Disposition:filename=tmp.csv ");
		$type = $this -> spArgs('type');
		$date_from = $this -> spArgs('date_from');
		$date_to = $this -> spArgs('date_to');
		$account = spClass("ddz_account");
		if ($date_from <> '') {
			$datelimit = " and date(gmt_create) between '$date_from' and '$date_to'";
		} else {
			$datelimit = '';
		}
		$fileds = "alipay_id,GMT_CREATE,GMT_LAST_VISIT";
		$rs = array();

		switch ($type) {
			case '0' :
				$rs = $account -> findall("date(now())-date(gmt_create)=1", null, $fileds);
				break;
			case '1' :
				$rs = $account -> findall("DATEDIFF(gmt_last_visit,gmt_create)=0" . $datelimit, "GMT_CREATE desc", $fileds);
				break;
			case '2' :
				$rs = $account -> findall("DATEDIFF(gmt_last_visit,gmt_create)=30" . $datelimit, "GMT_CREATE desc", $fileds);
				break;
			case '3' :
				$rs = $account -> findall("DATEDIFF(gmt_last_visit,gmt_create)>30" . $datelimit, "GMT_CREATE desc", $fileds);
				break;
			default :
				break;
		}
		foreach ($rs as $item) {
			$conditions = array("alipayid" => $item['alipay_id'], 'type' => $type);
			$count = spDB('tc_source') -> findcount($conditions);
			if ($count == 0) {
				$now = date("Y-m-d H:i:s", time());
				$new_souce = array("alipayid" => $item['alipay_id'], 'type' => $type, 'jfb' => 1, 'status' => '1', 'createdate' => $now);
				spDB('tc_source') -> create($new_souce);
			} else {
				continue;
			}

			// echo $item['alipay_id'] . ',' . '50,' . "\n";
		}
		echo "已导入营销表";
	}

	function syn() {
		
		//同步数据、统计业务指标
		$db = spDB('tc_source');
		$today = date("Y-m-d", time());
		$syn_condition = "";
		$rs_source = $db -> findall($syn_condition,'','');
		$updt_flag=uniqid();
		foreach ($rs_source as $item) {
			$alipayid = $item['alipayid'];
			
			$rs=spClass('tc_source')->spLinker()->find("alipayid='$alipayid'");
			if($rs){
				$ttl_trade=$rs['ttl_trade'];
				$ttl_price=$rs['ttl_price']['sum'];
				$ttl_rebates=$rs['ttl_rebates']['sum'];
				$last_trade=$rs['last_trade']['last'];
				$last_visit=$rs['ddz_account']['GMT_LAST_VISIT'];
				$conditions=array('alipayid'=>$alipayid);
				$updt=date("Y-m-d H:i:s",time());
				$up_source=array('ttl_trade'=>$ttl_trade,'ttl_price'=>$ttl_price,'ttl_rebates'=>$ttl_rebates,'last_trade'=>$last_trade,'updt'=>$updt,'updt_flag'=>$updt_flag,'last_visit'=>$last_visit);
				//echo "$alipayid:".$last_visit;
				$db->update($conditions,$up_source);
			}
		}
	}
	
	
	function extract_emails($str) {

	// This regular expression extracts all emails from a string:

	$regexp = '/([a-z0-9_.-])+@(([a-z0-9-])+.)+([a-z0-9]{2,4})+/i';

	preg_match_all($regexp, $str, $m);

	return isset($m[0]) ? $m[0] : array();

   }
	
	function extract_phone($str) {

	// This regular expression extracts all emails from a string:

	$regexp = '/[^0-9+](?P<tel>(\+86[0-9]{11})|([0-9]{11})|([0-9]{3,4}-[0-9]{7,10}))[^0-9+]/';

	preg_match_all($regexp, $str, $m);

	return isset($m[0]) ? $m[0] : array();

   }
	
	
	
	function test(){
		//$rs=spClass('tc_source')->spLinker()->find("alipayid='houjing520@126.com'");
		//dump($rs);
		// echo "aa";
		
		
		// $rs=$this->extract_emails($this->spArgs('source_txt'));
// 		
		// $prt='';
// 		
		// foreach ($rs as $item=>$value) {
			// $prt.=$value."<br/>";
		// }
// 		
		// echo $prt;

	}
	
	function ex_email(){
		
		$rs=$this->extract_emails($this->spArgs('source_txt'));
		
		$prt='';
		
		foreach ($rs as $item=>$value) {
			$prt.=$value."<br/>";
		}
		
		$rs2=$this->extract_phone($this->spArgs('source_txt'));
		foreach ($rs2 as $item2=>$value2) {
			$prt.=$value2."<br/>";
		}
		
		echo $prt;

	}
	
	
	
	function robot(){
		$this->display('find.html');
	}

}
